<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
       .title {
           float: left;
           cursor: pointer;
           padding:5px 20px ;
           border: 1px solid #cccccc;
           border-top-left-radius: 6px;
           border-top-right-radius: 6px;
           background-color: orange;
           color : honeydew;
       }
       .active{
           background-color: rgb(118, 118, 238);
           border-bottom:1px solid transparent;
       }
       .clear{
           clear : both;
       }
       #content {
           width: 500px;
           height:500px;
           padding: 10px;
           top: -1px;
           z-index: -1;
           border: 1px solid rgb(118, 118, 238);
           background:linear-gradient(rgb(118, 118, 238),#ffffff);
       }
       .input {
           display: block;
           width: 300px;
           height: 80px;
           line-height: 80px;
           border-radius: 6px;
           border: 1px solid #56a5f1;
           outline: none;
           margin: 0 auto;
           padding-left:20px ;
           font-size: 18px;
           color: tomato;
       }
    </style>
</head>
<body>
    <input type="text" class="input" placeholder="请输入音乐id" autofocus>
    <div class="title song active">单曲</div>
    <div class="title lyric">歌词</div>
    <div class="title playlist">其他</div>
    <div class="clear"></div>
    <div id="content"></div>
    <script>
        let contentlist = [];
        let indexPage = `这是首页的内容`;
        let newsPage = `这是新闻的内容`;
        let musicPage = `这是音乐的内容`;
        contentlist.push(indexPage,newsPage,musicPage);
        //console.log(content);可以直接通过id输出
        //默认显示首页内容
        content.innerHTML = contentlist[0];

        //选中按钮，查询选择器
        let btns= document.querySelectorAll('.title');

        //选取当前选中的元素
        let currentBtns = btns[0];

        for (let i=0 ; i < btns.length ; i++) {
            //通过for循环给每一个按钮增加属性并绑定事件
            let obj = btns[i];
            obj.indexE = i;//新增indexE属性，作为编号
            //绑定单击事件
            obj.onclick = function(e){
                console.log(e);
                console.log(e.target.indexE);
                //替换对应的内容
                content.innerHTML = contentlist[e.target.indexE];
                //移除class
                currentBtns.classList.remove('active');
                //给当前元素增加class
                //e.target.classList.add('active');
                this.classList.add('active');
                //更新当前元素
                currentBtns = this;
            }
        }
    </script>
</body>
</html>